﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace YKPrintService.Util
{
    /// <summary>
    /// 哈希签名工具类
    /// </summary>
    public class HashSignUtil
    {
        public static String sign(String signSource)
        {
            string rethash = "";
            try
            {
                
                System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
                System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
                byte[] combined = encoder.GetBytes(signSource);
                hash.ComputeHash(combined);
                rethash = byteToHexStr(hash.Hash, hash.Hash.Length);
            }
            catch (Exception ex)
            {
                string strerr = "Error in HashCode : " + ex.Message;
            }
            return rethash;

        }
        //字节数组转16进制字符串
        private static string byteToHexStr(byte[] bytes, int length)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                }
            }
            return returnStr.ToLower();
        }
    }
}
